package cn.straosp.workrecord.controller

import cn.straosp.workrecord.bean.Account
import cn.straosp.workrecord.bean.LoginAccount
import cn.straosp.workrecord.bean.RegisterAccount
import cn.straosp.workrecord.config.R
import cn.straosp.workrecord.service.AccountService
import cn.straosp.workrecord.util.Result
import jakarta.websocket.server.PathParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/account")
class AccountController {

    @Autowired
    lateinit var accountService: AccountService

    @PostMapping("/login")
    fun loginAccount(loginAccount: LoginAccount): R<Account> {
        val  result = accountService.login(loginAccount)
        return when(result){
            is Result.Error -> R.error(result.errorMessage)
            is Result.Success<Account> -> R.success(result.data)
        }
    }

    @PostMapping("/login")
    fun registerAccount(registerAccount: RegisterAccount): R<Account> {
        val  result = accountService.registerAccount(registerAccount)
        return when(result){
            is Result.Error -> R.error(result.errorMessage)
            is Result.Success<Account> -> R.success(result.data)
        }
    }

    @GetMapping("/{id}")
    fun getAccountById(@PathParam("id") accountId: Int): R<Account> {
        val result = accountService.getAccountById(accountId)
        return when(result){
            is Result.Error -> R.error(result.errorMessage)
            is Result.Success<Account> -> R.success(result.data)
        }
    }



}